/*
 * Copyright (c) 2010, 2011
 *	Open Source Technologies Ltd.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the company nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COMPANY AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <stdio.h>

#include "defs.h"
#include "token.h"
#include "lexer.h"

lexer_t *
lexer_create(const char *path)
{
	lexer_t *lexer;

	assert(path != NULL);

	lexer = (lexer_t *)malloc(sizeof(lexer_t));
	if (!lexer)
		return NULL;

	lexer->l_magic = LEXER_DEAD;
	if (!lexer_initialize(lexer, path)) {
		free(lexer);
		return NULL;
	}

	return lexer;
}

lexer_t *
lexer_initialize(lexer_t *lexer, const char *path)
{
	assert(is_lexer_dead(lexer));
	assert(path != NULL);

	memset(lexer, 0, sizeof(lexer_t));

	lexer->l_path = strdup(path);
	if (!lexer->l_path)
		return NULL;

	lexer->l_stream = fopen(path, "rt");
	if (!lexer->l_stream) {
		free(lexer->l_path);
		return NULL;
	}

	lexer->l_magic  = LEXER_MAGIC;
	lexer->l_row    = 1;
	lexer->l_column = 0;
	lexer->l_error  = le_success;

	return lexer;
}

token_t *
lexer_read_token(lexer_t *lexer)
{
	assert(is_lexer(lexer));

	return NULL;
}

#ifndef NDEBUG

char *
lexer_get_path(lexer_t *lexer)
{
	assert(is_lexer(lexer));
	return lexer->l_path;
}

int
lexer_get_row(lexer_t *lexer)
{
	assert(is_lexer(lexer));
	return lexer->l_row;
}

int
lexer_get_column(lexer_t *lexer)
{
	assert(is_lexer(lexer));
	return lexer->l_column;
}

int
lexer_get_error(lexer_t *lexer)
{
	assert(is_lexer(lexer));
	return lexer->l_error;
}

#endif /* !NDEBUG */

void
lexer_finalize(lexer_t *lexer)
{
	assert(is_lexer(lexer));

	if (lexer->l_path)
		free(lexer->l_path);
	if (lexer->l_stream)
		fclose(lexer->l_stream);

	lexer->l_magic = LEXER_DEAD;
}

lexer_t *
lexer_destroy(lexer_t *lexer)
{
	assert(is_lexer(lexer));

	lexer_finalize(lexer);
	free(lexer);

	return NULL;
}
